package com.pys.service;

import java.util.List;

import org.hibernate.Session;

import com.pys.dao.impl.OncelikDAO;
import com.pys.dao.impl.ProjeDAO;
import com.pys.pojo.PysTalepOncelik;
import com.pys.utils.HibernateUtil;

public class OncelikService {

	public PysTalepOncelik getById(long id) {
		Session session = null;
		PysTalepOncelik pysTalepOncelik = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			pysTalepOncelik = (PysTalepOncelik) oncelikDAO.findById(
					PysTalepOncelik.class, id);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return pysTalepOncelik;
	}

	public void save(PysTalepOncelik pysTalepOncelik) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			oncelikDAO.save(pysTalepOncelik);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void update(PysTalepOncelik pysTalepOncelik) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			oncelikDAO.update(pysTalepOncelik);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	// public void roleEkle(String roleAd) {
	// Session session = HibernateUtil.getSessionFactory().openSession();
	// session.beginTransaction();
	// OncelikDAO oncelikDAO = new OncelikDAOImpl(session);
	// PysTalepOncelik pysTalepOncelik = new PysTalepOncelik();
	// pysTalepOncelik.setAd(roleAd);
	// oncelikDAO.save(pysTalepOncelik);
	// session.getTransaction().commit();
	// session.close();
	// }

	public List<PysTalepOncelik> getAllList(Boolean deleted) {
		Session session = null;
		List<PysTalepOncelik> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			if (deleted == null)
				list = oncelikDAO.findAll(PysTalepOncelik.class);
			else
				list = oncelikDAO.findByProperty(PysTalepOncelik.class,
						"deleted", deleted);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public void delete(PysTalepOncelik pysTalepOncelik) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			oncelikDAO.delete(pysTalepOncelik);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<PysTalepOncelik> getByName(String name) {
		Session session = null;
		List<PysTalepOncelik> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			OncelikDAO oncelikDAO = new OncelikDAO(session);
			list = oncelikDAO.findByProperty(PysTalepOncelik.class, "name",
					name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public List<PysTalepOncelik> getAllAktifList() {
		Session session = null;
		List<PysTalepOncelik> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			ProjeDAO projeDAO = new ProjeDAO(session);
			list = projeDAO.findByProperties(PysTalepOncelik.class,
					new String[] { "deleted", "durum" }, new Object[] { false,
							true });
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

}
